import { Input, InputNumber } from "antd"
import { Rule } from "antd/es/form"
import { CSSProperties, FC, ReactNode } from "react"

import { SFormItem } from "@/components/common"
import { useLocale } from "@/locales"
import { notAllowSpecialCharacters, onlyPositiveNumber } from "@/utils/validate"

const NormalInput: FC<{
	labelId?: string
	name: string
	otherProps?: any
	allowValidate?: boolean
	validateNumber?: boolean
	isCollapsed?: boolean
	onChange?: (value: string) => void
	inputStyle?: CSSProperties
	rules?: Rule[]
	type?: "number" | "textarea"
	prefix?: ReactNode
	maxLength?: number
	showCount?: boolean
	count?: any
	precision?: number
	disabled?: boolean
	help?: string
	placeholder?: string
}> = props => {
	const { newFormat } = useLocale()

	const onNotAllow = (value: string) => {
		props.onChange && props.onChange(value)
	}

	return (
		<SFormItem
			name={props.name}
			label={props.labelId ? newFormat(props.labelId) : undefined}
			help={props.help}
			rules={props.rules}
		>
			{props.type && props.type === "textarea" ? (
				<Input.TextArea
					placeholder={newFormat(props.placeholder || "common.please_enter")}
					allowClear
					style={props.inputStyle}
					{...props.otherProps}
					showCount={props.showCount}
					maxLength={props.maxLength}
					disabled={props.disabled}
					autoComplete="off"
				/>
			) : props.type && props.type === "number" ? (
				<InputNumber
					placeholder={newFormat("common.please_enter")}
					allowClear
					{...props.otherProps}
					style={props.inputStyle}
					type={props.type}
					addonBefore={props.prefix}
					precision={props.precision}
					disabled={props.disabled}
					autoComplete="off"
					controls={false}
					{...props.otherProps}
				/>
			) : (
				<Input
					placeholder={newFormat(props.placeholder || "common.please_enter")}
					allowClear
					{...props.otherProps}
					style={props.inputStyle}
					prefix={props.prefix}
					disabled={props.disabled}
					autoComplete="off"
					onChange={e =>
						props.allowValidate &&
						(props.validateNumber ? onlyPositiveNumber(e, onNotAllow) : notAllowSpecialCharacters(e, onNotAllow))
					}
					{...props.otherProps}
				/>
			)}
		</SFormItem>
	)
}

export default NormalInput
